package com.jfl.service.impl.system;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.jfl.base.BaseServiceImpl;
import com.jfl.common.MessageKey;
import com.jfl.config.Resources;
import com.jfl.exception.BusinessException;
import com.jfl.mapper.SysDictDataMapper;
import com.jfl.mapper.SysDictTypeMapper;
import com.jfl.model.SysDictType;
import com.jfl.system.SysDictTypeService;
import com.jfl.utils.ConvertUtil;

/**
 * 
 * 数据字典
 * 
 * @author 赵炎
 * @version [V1.00, 2018年12月23日]
 * @since V1.00
 */
@Component
@Service(version = "${jfl.version}")
public class SysDictTypeServiceImpl extends BaseServiceImpl<SysDictType, SysDictTypeMapper> implements SysDictTypeService
{
    @Autowired
    private SysDictDataMapper sysDictDataMapper;
    
    @Override
    public String checkDictTypeUnique(SysDictType dictType)
    {
        Long id = dictType.getId() == null ? -1L : dictType.getId();
        SysDictType record = this.mapper.selectByDictType(dictType.getDictType());
        return result(record, id);
    }
    
    @Transactional
    @Override
    public void deleteByIds(String ids) throws BusinessException
    {
        Long[] arr = ConvertUtil.toLongArray(ids);
        for (Long id : arr)
        {
            SysDictType dictType = queryById(id);
            if (dictType != null)
            {
                int count = this.sysDictDataMapper.countByDictType(dictType.getDictType());
                if (count > 0)
                {
                    throw new BusinessException(String.format(Resources.getMessage(MessageKey.MESSAGE_FORMAT), dictType.getDictName()));
                }
            }
            this.mapper.deleteById(id);
        }
    }
    
}
